<?php
/**
 * @author Ngo Minh Tri <minhtri2711@gmail.com>
 */
require_once APPPATH."controllers/cmsbase.php";
class cms_store extends CmsBase {

	var $_require_login = true;
	var $controller_js_path = 'cms_store/main';

	function __construct(){
		parent::__construct();
		// Check permission
		parent::_check_permission('pms_store');
		$this->load->model('Model_store', 'model_store');
		$this->load->model('Model_type', 'model_type');
		$this->load->model('Model_mediaset', 'model_mediaset');
	}

	/**
	 *
	 *
	 */
	function index(){
		return $this->_render_my_view(array());
	}
	/**
	 *
	 *
	 */
	function browse_store(){
		$this->load->library('pagination');
		$stores = $this->model_store->fetch_all();

		$start = 0;
		$limit = 10;
		$total = count($stores);
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']):0;
		if($per_page){
			$start = $per_page;
		}
		$config['base_url'] = "?c=cms_store&m=browse_store";
		$config['uri_segment'] = 3;
		$config['total_rows'] = $total;
		$config['per_page'] = $limit;
		$this->pagination->initialize($config);

		return $this->load->view("cms_store/browse_store", array('stores'=>$stores));
	}
	/**
	 *
	 *
	 */
	function add_store(){
		$this->load->library('form_validation');
		// Validation
		if($_SERVER['REQUEST_METHOD'] === 'POST'){
			// Check is method POST
			$this->form_validation->set_rules('item_email', 'Email', 'trim|required|valid_email|callback_storeemail_check');
			if($this->form_validation->run()){
				$data = parse_input("item_", $_POST);
				// insert store type item
				if(is_array($data['types'])){
					$types = $data['types'];
				}
				unset($data['types']);
				$store_id = $this->model_store->insert_store($data);
				
				foreach($types as $val){
					$this->model_type->insertStoreType(array('store_id' => $store_id, 'type_id' => $val));
				}
				redirect('cms_store/index', 'refresh');
			}		
		}
		$types = $this->model_type->fetchAll();
		$galleries = $this->model_mediaset->browse($keyword, $fromdate, $todate);
		return $this->_render_my_view(array("types" => $types, 'galleries' => $galleries));
	}
	/**
	 *
	 *
	 */
	function edit_store($store_id){
		if(intval($store_id)){
			$this->load->library('form_validation');
			if($_SERVER['REQUEST_METHOD'] === 'POST'){
				// Check is method POST
				$this->form_validation->set_rules('item_email', 'Email', 'trim|required|valid_email|callback_storeemail_check');
				if($this->form_validation->run()){
					// validation
					$data = parse_input("item_", $_POST);
					// Transaction apply
					// delete store_type items by store_id
					$this->model_type->deleteByStoreId($store_id);
					// insert store type item
					if(is_array($data['types'])){
						foreach($data['types'] as $val){
							$this->model_type->insertStoreType(array('store_id' => $store_id, 'type_id' => $val));
						}
					}
					unset($data['types']);
					// update
					$this->model_store->update_store($store_id, $data);
					redirect('cms_store/index', 'refresh');
				}
			}
			$types = $this->model_type->fetchAll();
			$store_data = $this->model_store->find($store_id);
			$type_selected_arr = $this->model_type->fetchByStoreId($store_id);
			$type_selected = null;
			if(is_array($type_selected_arr)){
				foreach($type_selected_arr as $val)
					$type_selected[] = $val['type_id']; 
			}
			$galleries = $this->model_mediaset->browse($keyword, $fromdate, $todate);
			return $this->_render_my_view(array('store' => $store_data, "roles" => $roles, "types" => $types, 'galleries' => $galleries,"type_selected" => $type_selected));
		}
	}

	/**
	 *
	 *
	 */
	function find_store($store_id){
		$store_data = $this->model_store->find($store_id);
		return $this->load->view("cms_store/find_store", array('item' => $store_data));
	}

	/**
	 *
	 *
	 */
	function delete_store($store_id){
		$this->model_store->delete_store($store_id);
		exit(0);
	}

	/**
	 *
	 *
	 */
	function storeloginame_check($str){
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_store->check_loginname($str, $id);
		if (is_array($isCheck))
		{
			// Email already exists! Please input another email!
			$message = "Login name already exists! Please input another login name!";
			$this->form_validation->set_message('storeloginame_check', $message);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	/**
	 *
	 *
	 */
	function storeemail_check($str){
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_store->check_email($str, $id);
		if (!empty($isCheck))
		{
			// Email already exists! Please input another email!
			$message = "Email already exists! Please input another email!";
			$this->form_validation->set_message('storeemail_check', $message);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	/**
	 *
	 */
	function browse_type(){
		$types = $this->model_type->fetchAll();
		$total = count($types);
		return $this->load->view("cms_store/browse_type", array('types'=>$types));
	}
	/**
	 *
	 *
	 */
	function add_type(){
		if($_SERVER['REQUEST_METHOD'] === 'POST')
		{
			// validation
			$data = parse_input("item_", $_POST);
			$type_id = $this->model_type->insert_type($data);
			$type_data = $this->model_type->find($type_id);
			$type_data['status'] = 'success';
			echo json_encode($type_data);
			exit(0);
		}
		else{
			return $this->load->view("cms_store/add_type");
		}
	}

	/**
	 *
	 *
	 */
	function edit_type($type_id){
		if(intval($type_id)){
			if($_SERVER['REQUEST_METHOD'] === 'POST'){
				// validation
				$data = parse_input("item_", $_POST);
				if(isset($data['id'])) unset($data['id']);

				$this->model_type->update_type($type_id, $data);
				$type_data = $this->model_type->find($type_id);
				$type_data['status'] = 'success';
				echo json_encode($type_data);
				exit(0);
			}
			else{
				$type_data = $this->model_type->find($type_id);
				return $this->load->view("cms_store/edit_type", array('type' => $type_data));
			}
		}
	}

	/**
	 *
	 *
	 */
	function delete_type($type_id){
		$this->model_type->delete_type($type_id);
		exit(0);
	}
	/**
	 *
	 *
	 */
	function find_type($type_id){
		$type_data = $this->model_type->find($type_id);
		return $this->load->view("cms_store/find_type", array('item' => $type_data));
	}
}
?>
